package FindLast;

public class findLast {
	
	public static void main(String args[]) {
		// Initialize the array
		int[] x = {1, 4, 8, 9};
		int y = 1;
		
		// find the last element in which the value is present
		int result = findLast(x, y);
		
		// Print the array and the result
		System.out.print("Array: [ ");
		for(int z = 0; z < x.length - 1; z++)
			System.out.print(x[z] + " ");
		System.out.println("]\ny = " + y);
		System.out.println("Result: " + result);
	}
	
	public static int findLast(int[] x, int y) {
		// Effects: If x == null throw NullPointerException
		//   else return the index of the last element
		//   in x that equals y.
		//   If no such element exists, return -1.
		
		// Fixed fault from Section 1.2 #3 from i > 0
		for(int i = x.length-1; i >= 0; i--) {
			if (x[i] == y) {
				return i;
			}
		}
		
		return -1;
	}
}
